package cn.buk.tms.entity.bill;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.entity.misc.Customer;
import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.*;
import java.util.Date;

/**
 * 收款单：记录企业客户已支付的款项
 *
 * @author yfdai 2017/7/18
 */
@Entity
@Table(name = "ent_receipt")
public class ReceiptInfo {

    /**
     * 收款单
     */
    public final static int RECEIPT_TYPE_RECEIVE = 0;

    /**
     * 退款单
     */
    public final static int RECEIPT_TYPE_REFUND = 1;

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

    @Column(name = "enterprise_id")
    private int enterpriseId;

    /**
     * 收款单号
     */
    @Column(name = "receipt_no", length = 32)
    private String receiptNo;

    /**
     * 0/null - 收款单
     * 1 - 退款单
     */
    @Column(name = "receipt_type")
    private Integer receiptType;

    /**
     * 收款单收到的金额
     */
    private double amount;

    private String remark;

    /**
     * 客户类型
     * 0 - 代表散客
     * 1 - 代表企业客户
     */
    @Column(name="customer_type")
    private Integer customerType;


    /**
     * 客户在系统中的id: 在t102中对应的ID，是plutus录入时保存的
     */
    @ManyToOne
    @JoinColumn(name="customer_id")
    private Customer customer;


    /**
     * 创建此账单的用户名
     */
    @Column(length = 32)
    private String operator;

    /**
     * 收款银行
     */
    @Column(name = "receipt_bank", length = 50)
    private String receiptBank;

    /**
     * 收款日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    @Temporal(TemporalType.DATE)
    @Column(name="receipt_date")
    private Date receiptDate;


    /**
     * 销账金额
     */
    @Column(name = "writeoff_amount")
    private double writeOffAmount;

    /**
     * 0 - 未销账
     * 1 - 已销账：writeOffAmount = amount
     */
    private int status;

    /**
     * 订单保存数据库的时间：系统自动生成
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="create_time", insertable = false, updatable = false)
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(insertable = false)
    private Date lastUpdate;

    /**
     * 收款单余额
     * @return
     */
    public double getBalance() {
        double b = this.amount - this.writeOffAmount;
        return DecimalUtil.keepScale2(b);
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getReceiptNo() {
        return receiptNo;
    }

    public void setReceiptNo(String receiptNo) {
        this.receiptNo = receiptNo;
    }

    public double getAmount() {
        return DecimalUtil.keepScale2(amount);
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public double getWriteOffAmount() {
        return DecimalUtil.keepScale2(writeOffAmount);
    }

    public void setWriteOffAmount(double writeOffAmount) {
        this.writeOffAmount = writeOffAmount;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public int getCustomerType() {
        return customerType == null ? Customer.ENT_CORPORATION : customerType;
    }

    public void setCustomerType(Integer customerType) {
        this.customerType = customerType;
    }

    public String getReceiptBank() {
        return receiptBank;
    }

    public void setReceiptBank(String receiptBank) {
        this.receiptBank = receiptBank;
    }

    public Date getReceiptDate() {
        return receiptDate;
    }

    public void setReceiptDate(Date receiptDate) {
        this.receiptDate = receiptDate;
    }

    public int getReceiptType() {
        return receiptType == null ? 0 : receiptType;
    }

    public void setReceiptType(Integer receiptType) {
        this.receiptType = receiptType;
    }
}
